# Windows服务安装脚本 - 后台运行服务
param(
    [switch]$Install,
    [switch]$Uninstall,
    [switch]$Start,
    [switch]$Stop,
    [switch]$Status
)

$serviceName = "LzfyHisService"
$serviceDisplayName = "HIS退费审核服务"
$serviceDescription = "HIS退费审核管理系统后台服务"
$exePath = Join-Path $PSScriptRoot "bin\Release\net9.0\Lzfy_His_Service.exe"

function Install-Service {
    Write-Host "安装Windows服务..." -ForegroundColor Green
    
    # 检查exe是否存在
    if (-not (Test-Path $exePath)) {
        Write-Host "构建服务..." -ForegroundColor Yellow
        dotnet publish --configuration Release --output "bin\Release\net9.0"
    }
    
    # 创建服务
    $arguments = @(
        "create"
        $serviceName
        "binPath= `"$exePath`""
        "DisplayName= `"$serviceDisplayName`""
        "start= auto"
        "obj= LocalSystem"
    )
    
    & sc.exe $arguments
    
    # 设置服务描述
    & sc.exe description $serviceName $serviceDescription
    
    # 设置服务失败恢复策略
    & sc.exe failure $serviceName reset= 86400 actions= restart/60000/restart/60000/restart/60000
    
    Write-Host "服务安装完成" -ForegroundColor Green
    Write-Host "服务名称: $serviceName" -ForegroundColor Cyan
    Write-Host "显示名称: $serviceDisplayName" -ForegroundColor Cyan
}

function Uninstall-Service {
    Write-Host "卸载Windows服务..." -ForegroundColor Yellow
    
    # 停止服务
    Stop-Service -Name $serviceName -Force -ErrorAction SilentlyContinue
    Start-Sleep -Seconds 2
    
    # 删除服务
    & sc.exe delete $serviceName
    
    Write-Host "服务卸载完成" -ForegroundColor Green
}

function Start-Service {
    Write-Host "启动服务..." -ForegroundColor Green
    Start-Service -Name $serviceName
    Write-Host "服务已启动" -ForegroundColor Green
}

function Stop-Service {
    Write-Host "停止服务..." -ForegroundColor Yellow
    Stop-Service -Name $serviceName -Force
    Write-Host "服务已停止" -ForegroundColor Green
}

function Get-ServiceStatus {
    $service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
    if ($service) {
        Write-Host "服务状态: $($service.Status)" -ForegroundColor $(if ($service.Status -eq 'Running') { 'Green' } else { 'Red' })
        Write-Host "启动类型: $($service.StartType)" -ForegroundColor Cyan
    } else {
        Write-Host "服务未安装" -ForegroundColor Yellow
    }
}

# 检查管理员权限
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
    Write-Host "请以管理员身份运行此脚本" -ForegroundColor Red
    exit 1
}

# 根据参数执行操作
switch ($true) {
    $Install { Install-Service }
    $Uninstall { Uninstall-Service }
    $Start { Start-Service }
    $Stop { Stop-Service }
    $Status { Get-ServiceStatus }
    default { 
        Write-Host "使用方法:" -ForegroundColor Cyan
        Write-Host "  .\install-windows-service.ps1 -Install    # 安装服务" -ForegroundColor Yellow
        Write-Host "  .\install-windows-service.ps1 -Start     # 启动服务" -ForegroundColor Yellow
        Write-Host "  .\install-windows-service.ps1 -Stop      # 停止服务" -ForegroundColor Yellow
        Write-Host "  .\install-windows-service.ps1 -Status    # 查看状态" -ForegroundColor Yellow
        Write-Host "  .\install-windows-service.ps1 -Uninstall # 卸载服务" -ForegroundColor Yellow
    }
}